CXX = g++
CXXFLAGS = -std=c++11 -Wall -Wextra -Iinclude
SRCDIR = src
INCDIR = include
BUILDDIR = build
TARGET = main
ZIPNAME = project_submission.zip

SOURCES = $(wildcard $(SRCDIR)/*.cpp)
OBJECTS = $(SOURCES:$(SRCDIR)/%.cpp=$(BUILDDIR)/%.o)

all: $(TARGET)

$(TARGET): $(OBJECTS)
	@echo "链接目标文件生成可执行文件..."
	$(CXX) $(CXXFLAGS) $^ -o $@
	@echo "编译完成！可执行文件: $(TARGET)"

$(BUILDDIR)/%.o: $(SRCDIR)/%.cpp | $(BUILDDIR)
	@echo "编译 $<..."
	$(CXX) $(CXXFLAGS) -c $< -o $@

$(BUILDDIR):
	@echo "创建构建目录..."
	@mkdir -p $(BUILDDIR)

run: $(TARGET)
	@echo "运行程序..."
	@echo "=========================================="
	./$(TARGET)

debug: CXXFLAGS += -g -DDEBUG
debug: clean all

release: CXXFLAGS += -O2 -DNDEBUG
release: clean all

clean:
	@echo "清理构建文件..."
	rm -rf $(BUILDDIR) $(TARGET) $(ZIPNAME)
	@echo "清理完成！"

submit: all
	@echo "打包项目（包含可执行程序）..."
	@zip -r $(ZIPNAME) \
		include/ \
		src/ \
		Makefile \
		README.md \
		$(TARGET)
	@echo "打包完成！文件: $(ZIPNAME)"
	@echo "压缩包包含: 头文件、源文件、Makefile、README.md 和可执行程序 $(TARGET)"

info:
	@echo "=== 项目信息 ==="
	@echo "目标文件: $(TARGET)"
	@echo "源文件: $(SOURCES)"
	@echo "头文件目录: $(INCDIR)"
	@echo "构建目录: $(BUILDDIR)"
	@echo "编译器: $(CXX)"
	@echo "编译选项: $(CXXFLAGS)"

help:
	@echo "=== 可用命令 ==="
	@echo "  make all       - 编译项目（默认）"
	@echo "  make run       - 运行程序"
	@echo "  make debug     - 调试模式编译"
	@echo "  make release   - 发布模式编译"
	@echo "  make clean     - 清理构建文件"
	@echo "  make submit    - 编译并打包项目"
	@echo "  make info      - 显示项目信息"
	@echo "  make help      - 显示此帮助信息"

.PHONY: all run debug release clean submit info help